<?php
require_once ('Controller/Mobile/Common.inc.php');
require_once ('View/Result.inc.php');
require_once ('Model/MessageModel.inc.php');
require_once ('Model/DeviceToMessageModel.inc.php');

final class Controller extends common {
	private $Result;
	private $Loader;
	private $Callback = NULL;
	private $DeviceToMessageModel;
	private $MessageModel;
	public function __construct () {
		parent::__construct();
		$trace = debug_backtrace();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
	}

	public function execude () {
		header ('Content-Type: text/xml');
		$this->Result = new Result ();
		$this->MessageModel = new MessageModel ($this->mysql);
		$this->DeviceToMessageModel = new DeviceToMessageModel ($this->mysql);
		if (array_key_exists ('action', $_REQUEST)) {
			$this->getFunction ($_REQUEST['action']);
		} else {
			$this->defaultPage ();
		}
		print $this->Result->Create ($this->Callback);
	}

	public function getFunction ($action) {
		switch ($action) {
			case 'post':
				$this->post ();
				break;
			case 'getroomnumber':
				$this->getroomnumberPage ();
				break;
			case 'test':
				$this->test ();
				break;
			default:
				$this->defaultPage ();
				break;
		}
	}

	private $MessageCount = 0;
	private $Messageitem = [];
	private $pagesize = 20;
	private $page = 0;

	public function defaultPage () {
#		$this->DeviceToMessageModel->CleanUPMessage ();
#		$this->DeviceToCircleModel->CleanUPCircle ();

		if (array_key_exists ('to', $_REQUEST) && $_REQUEST['to'] != '') {
			if (($tocircleInfo = $this->CircleModel->CheckCircleItemExistByName ($_REQUEST['to'])) != NULL) {
				$toCircleID = $tocircleInfo['Circle_ID'];
				//echo $toCircleID;
			}
		}else{
			$toCircleID = NULL;
		}

		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL) {
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			$this->Callback = array ($this, 'getMessageCallBack');
			$this->MessageCount = $this->DeviceToMessageModel->GetMessageCount ($this->MobileInfo->DeviceID);
			if (array_key_exists ('page', $_REQUEST)) {
				if (ctype_digit ($_REQUEST['page']) && ($_REQUEST['page'] * $this->pagesize) <= $this->MessageCount) {
					$this->page = $_REQUEST['page'];
				} else {
					$this->Result->setErrorCode (1);
					return;
				}
			}
			$this->Messageitem =$this->DeviceToMessageModel->GetMessageItems ($this->MobileInfo->DeviceID, $this->page * $this->pagesize, $this->pagesize, $toCircleID);
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			$this->Callback = array ($this, 'getMessageCallBack');

		}
	}

	public function getMessageCallBack ($dom, $result) {
		$result->setAttribute ('Itemnum', $this->MessageCount);
		foreach ($this->Messageitem as $data) {
			$item = $dom->createElement ('item');
			foreach ($data as $key => $value) {
				$item->setAttribute ($key, $value);
			}
			$result->appendChild ($item);
		}
	}



	public function getroomnumberPage () {
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL) {
			$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			$this->Callback = array ($this, 'getRoomNumberCallBack');
		}
	}


	public function getRoomNumberCallBack ($dom, $result) {
		$Count = $this->CircleModel->GetCircleCount ();
		$result->setAttribute ('Itemnum', $Count);
		foreach ($this->CircleModel->GetCircleItems (0, $Count) as $item) {
			if ($item[$this->CircleModel->typeRowName] == 1) {
				$subitem = $dom->createElement ('item');
				$subitem->setAttribute ('name', $item[$this->CircleModel->nameRowName]);
				$result->appendChild ($subitem);

/*				$newnode = $node->cloneNode (true);
				if ($Circle_ID != NULL && $item[$this->CircleModel->idRowName] == $Circle_ID) {
					$newnode->setAttribute ('selected', 'selected');
				}
				$newnode->setAttribute ('value', $item[$this->CircleModel->idRowName]);
				$newnode->nodeValue = htmlspecialchars ($item[$this->CircleModel->nameRowName]);
				$node->parentNode->appendChild ($newnode);*/
			}
		}

/*		$result->setAttribute ('Itemnum', $this->MessageCount);
		foreach ($this->Messageitem as $data) {
			$item = $dom->createElement ('item');
			foreach ($data as $key => $value) {
				$item->setAttribute ($key, $value);
			}
			$result->appendChild ($item);
		}*/
	}

	public function Test () {
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL) {
			$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
			if (array_key_exists ('text', $_REQUEST) && $_REQUEST['text'] != '' && array_key_exists ('to', $_REQUEST) && $_REQUEST['to'] != '') {
				if (($tocircleInfo = $this->CircleModel->CheckCircleItemExistByName ($_REQUEST['to'])) != NULL) {
					echo $tocircleInfo['Circle_Name'];
				}
			}
		}
	}


	public function Post () {
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL) {
			$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
			if (array_key_exists ('text', $_REQUEST) && $_REQUEST['text'] != '' && array_key_exists ('to', $_REQUEST) && $_REQUEST['to'] != '') {
				if (($tocircleInfo = $this->CircleModel->CheckCircleItemExistByName ($_REQUEST['to'])) != NULL) {
//				if (($tocircleInfo = $this->CircleModel->CheckCircleItemExist ($toid)) != NULL) {
					if ($this->MobileInfo->Circles[0]->ID != $tocircleInfo[$this->CircleModel->idRowName]) {
//						if ($tocircleInfo[$this->CircleModel->idRowName] == 1 && $this->DeviceToCircleModel->CheckCircleAvailable ($tocircleInfo[$this->CircleModel->idRowName]) > 0) {
						if ($this->DeviceToCircleModel->CheckCircleAvailable ($tocircleInfo[$this->CircleModel->idRowName]) > 0) {
							$this->Result->setStatus (200);
							$this->Result->setErrorCode (0);
							$this->MessageModel->AddMessageItem ($_REQUEST['text'], $this->MobileInfo->Circles[0]->ID, $tocircleInfo[$this->CircleModel->idRowName]);
						} else {
							$this->Result->setErrorCode (401);
						}
					} else {
						$this->Result->setErrorCode (482);
					}
				} else {
					$this->Result->setErrorCode (404);
				}
			}
		}
/*		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('text', $_REQUEST) && $_REQUEST['text'] != '') {
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			$this->GuestBookModel->AddGuestBookItem ($_REQUEST['text']);
		} else {
			$this->Result->setErrorCode (1);
		}*/
	}
}
?>
